package com.ekeyman.securesave.action;

import java.io.File;

import org.springframework.security.core.context.SecurityContextHolder;

import com.ekeyman.securesave.security.EkeymanUserDetails;
import com.ekeyman.securesavelib.business.ResourceManager;
import com.ekeyman.securesavelib.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

public class UploadAction extends ActionSupport implements Preparable{
 	private static final long serialVersionUID = 4182921264768439535L;
	private File file;
    private String contentType;
    private String filename;
    private String openidurl;
    private String fileDescription;
    private String uploadPassphrase;
	private ResourceManager resourceManager;
	
	@Override
	/*
	 * Extracts the OpenID url from EkeymanUserDetails.
	 */
	public void prepare() throws Exception {
		EkeymanUserDetails ekeymanUserDetails = 
			(EkeymanUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		setOpenidurl(ekeymanUserDetails.getUsername());
	}
    
	public String execute() {
		String result = SUCCESS;
		
		User user = getResourceManager().getUser(getOpenidurl());
		
		if(user == null){
			result = ERROR;
		}
		else{
			try {
				getResourceManager().processUploadedFile(file, filename, contentType, user, uploadPassphrase, fileDescription);
			}
			catch(Exception e){
				result = ERROR;
			}
		}
		return result;
	}
    
    
    public void setUpload(File file) {
       this.file = file;
    }

    public void setUploadContentType(String contentType) {
       this.contentType = contentType;
    }

    public void setUploadFileName(String filename) {
       this.filename = filename;
    }

    public void setOpenidurl(String openidurl) {
		this.openidurl = openidurl;
	}

	public String getOpenidurl() {
		return openidurl;
	}

	public void setFileDescription(String fileDescription) {
		this.fileDescription = fileDescription;
	}

	public String getFileDescription() {
		return fileDescription;
	}

	public void setUploadPassphrase(String uploadPassphrase) {
		this.uploadPassphrase = uploadPassphrase;
	}

	public String getUploadPassphrase() {
		return uploadPassphrase;
	}

	public void setResourceManager(ResourceManager resourceManager) {
		this.resourceManager = resourceManager;
	}

	public ResourceManager getResourceManager() {
		return resourceManager;
	}





}
